package cn.wp.ewallet.common.service.impl;

import cn.wp.common.core.dto.WpResult;
import cn.wp.common.core.util.BeanUtil;
import cn.wp.ewallet.common.dao.PubItemDictDao;
import cn.wp.ewallet.common.extension.constant.EwalletConstant;
import cn.wp.ewallet.common.model.dto.PubItemDictDto;
import cn.wp.ewallet.common.model.entity.PubItemDictDo;
import cn.wp.ewallet.common.service.PubItemDictService;
import cn.wp.ewallet.wallet.extension.constant.EwalletWalletConstant;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;


/**
 * 数据字典项服务类-业务逻辑层实现
 * Created by ganjie on 2020/01/09
 */
@Service
public class PubItemDictServiceImpl implements PubItemDictService {

    @Autowired
    private PubItemDictDao pubItemDictDao;

    @Override
    @Transactional
    public WpResult modifyHandlingFee(BigDecimal transFee, BigDecimal mentionFee) {
        //更新转账手续费
        pubItemDictDao.updateTransfee(transFee.divide(new BigDecimal(100)));
        //更新提币手续费
        pubItemDictDao.updatementionFee(mentionFee.divide(new BigDecimal(100)));
        return new WpResult().success();
    }

    @Override
    public List<PubItemDictDto> getPubItemDict(String dicId) {
        List<PubItemDictDo> pubItemDictDos = pubItemDictDao.selectPubItemDictByDicId(dicId);
        List<PubItemDictDto> pubItemDictDtos = BeanUtil.listCopyProperties(pubItemDictDos, PubItemDictDto.class);
        return pubItemDictDtos;
    }

    @Override
    public PubItemDictDto getPubItemDictByCode(String dicId, String code) {
        return BeanUtil.copyProperties(pubItemDictDao.selectPubItemDictByCode(dicId, code), PubItemDictDto.class);
    }

    @Override
    public String getTransFee(BigDecimal amount) {
        PubItemDictDto dictDto = getPubItemDictByCode(EwalletWalletConstant.FEE_DIC_ID, EwalletWalletConstant.FEE_TRANS);
        BigDecimal result = amount.multiply(BigDecimal.valueOf(Double.valueOf(dictDto.getItemValue())));
        return result.toString();
    }

    @Override
    public String getMentionFee(BigDecimal amount) {
        PubItemDictDto dictDto = getPubItemDictByCode(EwalletWalletConstant.FEE_DIC_ID, EwalletWalletConstant.FEE_MENTION);
        BigDecimal result = amount.multiply(BigDecimal.valueOf(Double.valueOf(dictDto.getItemValue())));
        return result.toString();
    }

    @Override
    public WpResult modifyItemValueByItemCode(String json) {
        JSONArray jsonArray = JSON.parseArray(json);
        for(int i = 0 ; i < jsonArray.size(); i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String itemCode = jsonObject.getString("itemCode");
            String itemValue = jsonObject.getString("itemValue");
            PubItemDictDo pubItemDictDo = new PubItemDictDo();
            pubItemDictDo.setItemCode(itemCode);
            pubItemDictDo.setItemValue(itemValue);
            pubItemDictDao.updateItemValueByItemCode(pubItemDictDo);
        }
        return new WpResult().success();
    }


}

